<?
session_start();
$gw_payex = "";
$pagename=LA_PAYMENT;

include_once("header_inc.php");
include_once("member_header.php");
include_once("payfunctions.php");
include_once("includes/functions-fdv.php");

check_valid_user("");

$tplPay = new TplLoad();
$step = getParam("step",0); //JMR: dividimos en dos pasos esta parte

$isFree = false; //variable que indica si es gratis o no el paquete
if ($_REQUEST["submit"] || $_REQUEST["final"])
{
	$tplPay->assign("submit",1);	
	$tplPay->assign("pid",getParam("pid",""));	
	$isFree =isFreePackage(getParam("pid",-1));
	$tplPay->assign("isFree",$isFree); 
	$tplPay->assign("cantidad",getParamInt("cantidad",1));	
	
	$url = "http://".$set_url . "/buy_r.php";
	if ($_REQUEST["final"])
	{
		$_SESSION['package_selected_id'] = getParamInt("pid","");
		if(!$isFree){
			 
			$sql = "select * from $pkg_tbl where pid=" . getParamInt("pid","");
			$res = q($sql);
			$c=0;
			$row=mysql_fetch_assoc($res);
			$extra = isExtraMainType($row['mainType']);
			if($extra){
				$tplPay->assign("extra",1);	
			}
			if (getParamInt("pay",0) > 0)
			{
				$o_id=makeOrder(getParamInt("pid",""),$_SESSION["valid_user"],time(),$row["pprice"],getParamInt("cantidad",1));
				$_SESSION['transaction_oid']=$o_id;
				if (getParam("pay",""))
					q("update $pay_tbl set o_vendor=" . getParamInt("pay","") . " where o_id=$o_id");
				
				if ($_REQUEST["pay"]==1)
					$urlR=buyPayPal($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_paypal_account);
				elseif ($_REQUEST["pay"]==2)
					$urlR=buy2CheckOut($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_2checkout_account);
				elseif ($_REQUEST["pay"]==3)
					$urlR=buyWorldPay($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_worldpay_account,$set_buy_worldpay_account_inst);
				elseif ($_REQUEST["pay"]==4)
					$urlR=buyAuthorize($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_autorize_account);
				elseif ($_REQUEST["pay"]==6)
					$urlR=buyBeanStream($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_beanstreem_account);
				elseif ($_REQUEST["pay"]==7)
					$urlR=buySecPay($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_secpay_account);
				elseif ($_REQUEST["pay"]==8)
					$urlR=buySecTrading($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_sectrading_account);
				elseif ($_REQUEST["pay"]==9)
					$urlR=buyNochex($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_nochex_account);
				elseif ($_REQUEST["pay"]==98)
				{
					$tplPay->assign("manual_payment",1);
					$tplPay->assign("price",$row["pprice"]);				
					$total=sprintf("%.2f",(float)($row["pprice"]*getParamInt("cantidad",1)));
					$tplPay->assign("total",$total);
					$tplPay->assign("pname",$row["pname"]);
					$tplPay->assign("pcurrency",$row["pcurrency"]);
					$tplPay->assign("oid",$o_id);
					
					//$url=buyBeanStream($row["pprice"],$o_id,$row["pname"],$row["pcurrency"],3,$url,$set_buy_beanstreem_account);
				}
				
				if ($set_inform_admin_order)
				{
					$ip=getRemoteIp();
					$subject = formatString(LA_PAY_INFORM_SUBJECT,array($_SESSION["user_name"],$row["pprice"],$urlR,$ip,formatDateShort(time())));
					$body = formatString(LA_PAY_INFORM_MESSAGE,array($_SESSION["user_name"],$row["pprice"],$urlR,$ip,formatDateShort(time())));
					
					sendEmail($set_webmaster_address,$set_webmaster_address,$subject,$body);
					
				}
				
				if ($urlR)
					echo "<script language=\"javascript\">window.location=\"$urlR\"</script>";
			}
			
		}else{
			//logMsg("paquete gratis","entré en paquete gratis");
			$packageIdFree = getParamInt("pid","");
			$o_id=makeOrder($packageIdFree,$_SESSION["valid_user"],time(),$row["pprice"]);
			//logMsg("paquete gratis -- make order","order creada: ".$o_id);		
			addPackage($_SESSION["valid_user"],$packageIdFree,$o_id);
			//logMsg("paquete gratis","addPackage invocado ");		
			$tplPay->assign("freePackageSelected",1);
		}
	}
}
/***********************************/
$catId = getParamInt("catid","");
$extraPackages = getParamInt("extra",0);
$concesionaria = getParamInt("concesionaria",0);

if($catId==''){
	echo "<script type=\"text/javascript\">top.location='choose_cat.php'</script>";	
}

$mainType=getPaqueteMainType($catId); //si este parametro es uno cambiamos MAIN TYPE por MAIN TYPE de Extra paquetes
if($extraPackages==1){
	$tplPay->assign("extra",1);	
	$mainType = getExtraMainType($mainType);
	
}
//JMR: Query modificado para omitir el paquete gratuito si este ya fue adquirido FREE_PACKAGE_COUNT veces 
$sql = "select  a.pid,a.pname,a.pterm, a.pdur, a.pactive, a.pprice, a.pcurrency, a.pupload, a.pmessages, a.pchat, a.pgb, a.protation, a.pvisitor,
    a.pfav, a.pmatch, a.pflirts, a.ppremium, a.mainType, a.concesionaria, a.avisos, count(a.pid) as countp 
from mvial_packages a left join mvial_orders b on a.pid=b.o_package and b.o_user=".$_SESSION["valid_user"]." where a.pactive=1 and a.mainType=$mainType 
and a.concesionaria=$concesionaria group by a.pid, a.pname, a.pterm, a.pdur, a.pactive, a.pprice, a.pcurrency, a.pupload, a.pmessages, a.pchat, a.pgb, a.protation, a.pvisitor,
    a.pfav, a.pmatch, a.pflirts, a.ppremium, a.mainType, a.concesionaria, a.avisos having pprice!=0 or countp<".FREE_PACKAGE_COUNT;
/**************/
$res = q($sql);
$c=0;
while($row=mysql_fetch_assoc($res))
{
	$array[$c]["pname"]=str_replace("/", "<br/>", $row["pname"]);
	$array[$c]["pid"]=$row["pid"];
	$array[$c]["counter"]=$c;
	$array[$c]["pterm"]=$row["pterm"];	
	$array[$c]["pdur"]=$row["pdur"];	
	$array[$c]["pactive"]=$row["pactive"];	
	$array[$c]["pprice"]=$row["pprice"];	
	$array[$c]["pcurrency"]=$row["pcurrency"];	
	$array[$c]["pupload"]=$row["pupload"];
	$array[$c]["mainType"]=$row["mainType"];
	$array[$c]["concesionaria"]=$row["concesionaria"];
	$array[$c]["avisos"]=$row["avisos"];	
	
	$array[$c]["pmessages"]=$row["pmessages"];	
	$array[$c]["pchat"]=$row["pchat"];	
	$array[$c]["pgb"]=$row["pgb"];	
	$array[$c]["protation"]=$row["protation"];	
	$array[$c]["ppremium"]=$row["ppremium"];	
	$array[$c]["pfav"]=$row["pfav"];	
	$array[$c]["pmatch"]=$row["pmatch"];	
	$array[$c]["pflirts"]=$row["pflirts"];	
		
	$c++;
}

$hay_concesionaria=0;
if($concecionaria==0){
	$sql2 = "select count(*) as concesionaria from $pkg_tbl where pactive=1 and mainType=$mainType and concesionaria=1";
	$res2 = q($sql2);
	if($row2=mysql_fetch_assoc($res2))
	{
		$hay_concesionaria=	$row2["concesionaria"];
	}
}

/*JMR: Verificación de creditos existentes*/
$hayCreditosViejos=obtenerCreditosExistentesPorCategoria($mainType,$_SESSION["valid_user"]);
if($hayCreditosViejos<1 && $step == 0){
		$step =1; //salteamos este paso ya que no hay creditos
}
$tplPay->assign("step",$step);
/****************************************/
$tplPay->assign("package_list", $array);
$tplPay->assign("concesionaria", $concesionaria);
$tplPay->assign("hay_concesionaria", $hay_concesionaria);
$tplPay->assign("catId",$catId);
$tplPay->assign("paypal_account","$set_buy_paypal_account");
$tplPay->assign("2checkout_account","$set_buy_2checkout_account");
$tplPay->assign("worldpay_account","$set_buy_worldpay_account");
$tplPay->assign("autorize_account","$set_buy_autorize_account");
$tplPay->assign("beanstreem_account","$set_buy_beanstreem_account");
$tplPay->assign("secpay_account","$set_buy_secpay_account");
$tplPay->assign("securetr_account","$set_buy_sectrading_account");
$tplPay->assign("nochex_account","$set_buy_nochex_account");
$tplPay->assign("manual","$set_buy_manual");
$tplPay->assign("hayCreditosViejos",$hayCreditosViejos);

$tplPay->display("payment_options.tpl");
include_once("member_footer.php");
include_once("footer_inc.php");
?>